<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * Core Exception / Error Handling Script
 * Started: August 22, 2009 10:33:35 AM
 * Script version: 1.0
 +------------------------------------
 * Exception System adapted from Woltlab Community Framework, Woltlab GmbH
 * (Licensed under the GNU LGPL)
 */

// Create a interface that will be implemented later by handleException and handleError
interface ExceptionInterface{
	public function show();
}

class SimpleException extends Exception implements ExceptionInterface{
	public function __construct($message = ''){
		parent::__construct($message, 0);
	}
	
	public function show(){
?>
<html>
<head>
<title>Creative Bulletin Board Core Error: <?php echo htmlspecialchars($this->getMessage()); ?></title>
<style type="text/css">
body {
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 0.8em;
}
div {
	border: 1px outset #CCC;
	padding: 3px;
	background-color: #f7f7f7;
}

div div {
	border: 1px inset #CCC;
	padding: 4px;
}

h1 {
	background-color: #B7EEBB;
	padding: 4px;
	color: #fff;
	margin: 0 0 3px 0;
	font-size: 1.15em;
}
h2 {
	font-size: 1.1em;
	margin-bottom: 0;
}

pre, p {
	margin: 0;
}
</style>
</head>
<body>
	<div>
		<h1>Creative Bulletin Board error: <?php echo htmlspecialchars(strip_tags($this->getMessage())); ?></h1>
	
		<div>
			<h2>Information:</h2>
			<p>A fatal error in Creative Bulletin Board was found and the script isn't able to continue. Please contact the administrator of this forum to report
			this problem. If you are the administrator, you can consult the CBB documentation for details on how to solve this problem.</p>
			<h2>We are sorry for the inconvenenience</h2>
		</div>
	</div>
</body>
</html>

<?php
	}
}

class CoreException extends Exception implements ExceptionInterface{
	protected $description;
	protected $information = '';

	public function __construct($message = '', $code = 0, $description = '', $type = 0) {
		parent::__construct($message, $code);
		$this->description = $description;
		$this->type = $type;
	}

	public function getDescription() {
		return $this->description;
	}
	
	public function getType(){
		return $this->type;
	}

	public function show() {
		?>
<html>
<head>
<title>Creative Bulletin Board Core Error: <?php echo htmlspecialchars($this->getMessage()); ?></title>
<style type="text/css">
body {
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 0.8em;
}
div {
	border: 1px outset #CCC;
	padding: 3px;
	background-color: #f7f7f7;
}

div div {
	border: 1px inset #CCC;
	padding: 4px;
}

h1 {
	background-color: #FF9300;
	padding: 4px;
	color: #fff;
	margin: 0 0 3px 0;
	font-size: 1.15em;
}
h2 {
	font-size: 1.1em;
	margin-bottom: 0;
}

pre, p {
	margin: 0;
}
</style>
</head>
<body>
	<div>
		<h1>Fatal error: <?php echo htmlspecialchars(strip_tags($this->getMessage())); ?></h1>
	
		<div>
			<p><?php echo $this->getDescription(); ?></p>

			<h2>Information:</h2>
			<p>
				<b>error message:</b> <?php echo htmlspecialchars(strip_tags($this->getMessage())); ?><br />
				<b>error code:</b> <?php echo intval($this->getCode()); ?><br />
				<?php echo $this->information; ?>
				<b>file:</b> <?php echo htmlspecialchars($this->getFile()); ?> (<?php echo $this->getLine(); ?>)<br />
				<b>php version:</b> <?php echo htmlspecialchars(phpversion()); ?><br />
				<b>date:</b> <?php echo gmdate('r'); ?><br />
				<b>request:</b> <?php if (isset($_SERVER['REQUEST_URI'])) echo htmlspecialchars($_SERVER['REQUEST_URI']); ?><br />
				<b>referer:</b> <?php if (isset($_SERVER['HTTP_REFERER'])) echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?><br />
			</p>
			
			<h2>Stacktrace:</h2>
			<pre><?php echo htmlspecialchars($this->getTraceAsString()); ?></pre>
			
			<h2>We are sorry for the inconvenenience</h2>
		</div>
	</div>
</body>
</html>

<?php
	}
}

function handleException(Exception $e){
	try{
		if($e instanceof ExceptionInterface){
			$e->show();
			exit;
		}
		print $e;
	}
	catch (Exception $e){}
}

function handleError($errorNo, $message, $filename, $lineNo){ 
	if (error_reporting() != 0) {
		$type = 'error';
		switch ($errorNo) {
			case 2: $type = 'warning';
				break;
			case 8: $type = 'notice';
				break;
		}

		throw new CoreException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
	}
	else{
		throw new SimpleException("PHP Error");
	}
}
